from heap.class_klass import HeapObject
from insts.inst import Index16Instruction


class INSTANCE_OF(Index16Instruction):

    def execute(self, frame):
        stack = frame.getOperandStack()
        ref: HeapObject = stack.popRef()
        if ref is None:
            stack.pushInt(0)
        else:
            cp = frame.getMethod().getClass().getConstantPool()
            classRef = cp.getConstantValue(self.index)
            klass = classRef.resolveClass()
            if ref.isInstanceOf(klass):
                stack.pushInt(1)
            else:
                stack.pushInt(0)
